package com.yijiaw.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yijiaw.domain.Message;
import com.yijiaw.service.MessageService;

@Controller
@RequestMapping("/admin")
public class MessageController {

	@Autowired
	MessageService messageService;
	
	@RequestMapping(value = "/message", method = RequestMethod.GET)
	public String messge(Model model) {
		List<Message> list = messageService.findAllMessages(0, messageService.countMessages());
		model.addAttribute("list", list);
		model.addAttribute("title", "留言");
		model.addAttribute("target", "message");
		
		return "admin/message_list";
	}
	
	
	@RequestMapping("/message/view/{id}")
	public String view(@PathVariable int id, Model model) {
		Message message = messageService.findMessageByPrimaryKey(id);
		model.addAttribute("message", message);
		
		return "admin/message_view";
	}
	
	@RequestMapping("/message/delete/{id}")
	public String delete(@PathVariable int id) {
		Message message = messageService.findMessageByPrimaryKey(id);
		if (message != null) {
			messageService.deleteMessage(message);
		}
		return "redirect:../../message";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
